﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace SqliteWrapper.Procedures
{
    public class Proc_DeleteAlbum : IProcedure
    {
        private bool deleteBook;
        public Proc_DeleteAlbum(bool delBook)
            : base()
        {
            deleteBook = delBook;
        }

        public override SqliteError doProcedure(object DataSource, string DBPath)
        {
            Album album = DataSource as Album;
            SqliteError error = new SqliteError() { IsProcessSuccess = false, ErrorText = null };
            SqliteError bk_update_error = new SqliteError() { IsProcessSuccess = false, ErrorText = null };
            SqliteConnectionManager manager = SqliteConnectionManager.Create(DBPath);
            using (SQLiteWriteLock writeLock = new SQLiteWriteLock(manager.ConnectionString))
            {
                using (SQLiteConnection conn = manager.GetSqliteConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "delete from Album where ID = @ID";
                        cmd.Parameters.Add("@ID", System.Data.DbType.Int64).Value = album.ID;
                        try
                        {
                            cmd.ExecuteNonQuery();
                            using (SQLiteTransaction trans = conn.BeginTransaction())
                            {
                                album.BookList.ForEach(book =>
                                    {
                                        if(!deleteBook)
                                        {
                                            book.AlbumId = -1;
                                            using (SQLiteCommand subCmd = conn.CreateCommand())
                                            {
                                                subCmd.CommandText = "update Book set AlbumId = @AlbumId where ID = @ID)";
                                                subCmd.Parameters.Add("@ID", System.Data.DbType.Int64).Value = book.ID;
                                                subCmd.Parameters.Add("@AlbumId", System.Data.DbType.Int64).Value = book.AlbumId;
                                                subCmd.ExecuteNonQuery();
                                            }
                                        }
                                        else
                                        {
                                            using(SQLiteCommand subCmd = conn.CreateCommand())
                                            {
                                                subCmd.CommandText = "delete from Book where ID = @ID";
                                                subCmd.Parameters.Add("@ID",System.Data.DbType.Int64).Value = book.ID;
                                                subCmd.ExecuteNonQuery();
                                            }
                                        }
                                    });
                                try
                                {
                                    trans.Commit();
                                    bk_update_error.IsProcessSuccess = true;
                                }
                                catch (Exception ex)
                                {
                                    bk_update_error.ErrorText = ex.Message;
                                }
                            }
                            if (!bk_update_error.IsProcessSuccess)
                            {
                                error.ErrorText = bk_update_error.ErrorText;
                            }
                        }
                        catch (Exception ex)
                        {
                            error.ErrorText = ex.Message;
                        }
                    }
                }
            }
            return error;
        }
    }
}
